﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Objects;

namespace CustomerService
{
    public partial class NewRefund : System.Web.UI.Page
    {
        private SalesManagerEntities _sme;

        protected void Page_Load(object sender, EventArgs e)
        {
            _sme = (SalesManagerEntities)Session["sme"];
            int orderID = int.Parse(Request.QueryString["OrderID"]);
            tblOrderHeader oh = _sme.tblOrderHeader.Find(new object[] {int.Parse(Request.QueryString["OrderID"])});
            if (!IsPostBack)
            {

                if (txtLoaded.Value != "1")
                {
                    ObjectResult<tblRefund> refunds = _sme.spNewRefunds(orderID, true);
                    txtLoaded.Value = "1";

                    ListView1.DataSource = refunds;
                    ListView1.DataBind();
                }
                else
                {
                    var v = (from p in _sme.tblRefund
                             where p.OrderID == orderID
                                 && p.TransactionID == null
                             select p).ToList();

                    ListView1.DataSource = v;
                    ListView1.DataBind();
                }

                ddRefundReason.DataSource = (from p in _sme.codeRefundType
                                            where (bool)p.Active &&
                                                (p.OwnerID == oh.OwnerID || p.OwnerID == null)
                                            select p).ToList();
                ddRefundReason.DataTextField = "Description";
                ddRefundReason.DataValueField = "RefundTypeID";
                ddRefundReason.DataBind();
            }
            else
            {
                // Put the qty values back into the ReturnDetail table
                for (int i = 0; i < ListView1.Items.Count; i++)
                {
                    int key = (int)ListView1.DataKeys[i].Value;
                    decimal cost;
                    if (decimal.TryParse(((TextBox)ListView1.Items[i].FindControl("txtCost")).Text, out cost))
                    {
                        tblRefund refund = _sme.tblRefund.Find(new object[] {key});
                        refund.Cost = cost;
                        int refundTypeID;
                        if (int.TryParse(ddRefundReason.SelectedValue, out refundTypeID))
                            refund.RefundTypeID = refundTypeID;
                    }
                }
            }
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            int orderID = int.Parse(Request.QueryString["OrderID"]);

            // Remove refund records that don't have any cost.
            var v = (from p in _sme.tblRefund
                     where p.OrderID == orderID
                         && p.TransactionID == null
                     select p).ToList();

            foreach (tblRefund refund in v)
            {
                if (refund.Cost == 0)
                    _sme.tblRefund.Remove(refund);
                if (refund.Cost > refund.MaxCost)
                    refund.Cost = refund.MaxCost;
            }

            //_sme.SaveChanges();

            // Trigger the RMAReturn panel to refresh.            
            if (!Page.ClientScript.IsStartupScriptRegistered("PostBack"))
                Page.ClientScript.RegisterStartupScript(this.GetType(), "PostBack",
                    "<script  language='javascript' type='text/javascript'>" +
                    "opener.__doPostBack('" + Request.QueryString["PanelID"] + "', 'UpdateDetails'); window.close('fred');" +
                    "</script>"
                    );
        }
    }
}